package capricornus.actor.res
{
	import capricornus.actor.Actor;
	import capricornus.actor.attribute.ActorAttribute;
	import capricornus.actor.events.ActionImageEvent;
	import capricornus.actor.events.ActorEvent;
	import capricornus.actor.res.ActionImage;
	import capricornus.actor.res.ActorImageManager;
	import capricornus.actor.utils.ActionType;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	public class ActionImageController
	{
		private var _actionImagePath:String = "";
		protected var _currentActionType:int = 0;

		private var _lastUrl:String = "";
		protected var _currentActionImage:ActionImage;
		
		protected var _bitmap:Bitmap;
		protected var _dictImage:Dictionary;
		
		public function ActionImageController( actionImagePath:String )
		{
			_actionImagePath = actionImagePath;
			init();
		}
		
		private function init():void
		{
			_dictImage = new Dictionary();
			_bitmap = new Bitmap();
		}
		
		public function destroy():void
		{
			_bitmap.bitmapData = null;
			for( var i:* in _dictImage )
			{
				var ai:ActionImage = _dictImage[i];
				if( ai != null ) ai.destroy();
				delete _dictImage[i];
			}
			if( _lastUrl != "" && _lastUrl != null )
			{
				ActorImageManager.instance.removeEventListener( _lastUrl, onActionImageLoaded );
				_lastUrl = "";
			}
		}
		
		public function get bitmap():Bitmap
		{
			return _bitmap;
		}
		
		public function set actionImageName( value:String ):void
		{
			if( _actionImagePath != value )
			{
				_actionImagePath = value;
				destroy();
				_currentActionImage = null;
				playAction( _currentActionType );
			}
		}
		
		/**
		 * 
		 * @param actionType 播放的动作
		 * @param times      播放次数
		 * 
		 */		
		public function playAction( actionType:int ):void
		{
			if( _actionImagePath=="" ) return;
			
			_currentActionType = actionType;
			_currentActionImage = _dictImage[_currentActionType];
			if( _currentActionImage==null )
			{
				_currentActionImage =  ActorImageManager.instance.getActionImage( _actionImagePath, _currentActionType );
				if( _currentActionImage==null )
				{
					//_currentActionImage = ActionImage.defaultActionImage();
					if( _lastUrl != "" && _lastUrl != null )
					{
						ActorImageManager.instance.removeEventListener( _lastUrl, onActionImageLoaded );
					}
					_lastUrl = _actionImagePath + _currentActionType;
					ActorImageManager.instance.addEventListener( _lastUrl, onActionImageLoaded, false, 0, true );
				}
				else
				{
					_dictImage[_currentActionType] = _currentActionImage;
				}
			}
		}
		
		private function onActionImageLoaded( e:ActionImageEvent ):void
		{
			var type:int = e.actionType;
			var ai:ActionImage =  ActorImageManager.instance.getActionImage( _actionImagePath, type );
			_dictImage[type] = ai;
			
			if( type==_currentActionType )
			{
				_currentActionImage = ai;
			}
		}
		
		public function nextFrame( direction:int, frameIndex:int ):void
		{
			if( _currentActionImage != null )
			{
				_bitmap.bitmapData = _currentActionImage.getImage( direction, frameIndex );
				var arr:Array = _currentActionImage.getOffset( direction, frameIndex );
				_bitmap.x = int(arr[0]);
				_bitmap.y = int(arr[1]);
			}
		}
	}
}





















